﻿using System;
using System.Collections.Generic;
using App_Code.Common;
using App_Code.Objects;
using Telerik.Web.UI;

namespace App_Code.Logic
{
    /// <summary>
    /// Summary description for ConnectionLogic
    /// </summary>
    public class ConnectionLogic
    {
        #region Public methods
        public static void AddUpdateConnection(VisualCronSession session, string connectionId, string host, string username, string password, string port, bool isLocalServer, bool useCompression)
        {
            VCConnection connection = null;
            int intPort;
            if (!int.TryParse(port, out intPort))
            {
                 intPort = Constants.VC_CONNECTION_DEFAULT_PORT;
            }

            if (!String.IsNullOrEmpty(connectionId))
            {
                connection = session.Connections.Find(c => c.ConnectionId.ToString() == connectionId);
            }

            if(connection != null)
            {
                connection.Host = host;
                connection.Username = username;
                connection.Password = password;
                connection.Port = intPort;
                connection.IsLocalServer = isLocalServer;
                connection.UseCompression = useCompression;
            }
            else
            {
                connection = new VCConnection(host, username, password, intPort, isLocalServer, useCompression);

                session.Connections.Add(connection);
            }

            if (session.Profile != null)
            {
                UserProfileLogic.AddUpdateConnection(session.Profile, connection);

                UserProfileLogic.UpdateProfileConnections(session.Profile);

                SessionLogic.ProfileConnectionsToSession(session);
            }

            session.ConnectionStateChanged();

        }

        public static void RemoveConnection(VisualCronSession session, string connectionId)
        {
            var connection = session.Connections.Find(c => c.ConnectionId.ToString() == connectionId);

            if (connection == null || session.Connections.Count < 2)
                return;

            session.Connections.RemoveAll(c => c.ConnectionId.ToString() == connectionId && c.Connected == false);

            if (session.Profile != null)
            {
                UserProfileLogic.RemoveConnection(session.Profile, connection);

                UserProfileLogic.UpdateProfileConnections(session.Profile);

                SessionLogic.ProfileConnectionsToSession(session);
            }

            session.ConnectionStateChanged();

        }

        public static void ServerConnectionAdded(VisualCronSession session, string connectionId, string userServerId)
        {
            var connection = session.Connections.Find(c => c.ConnectionId.ToString() == connectionId);

            if (connection != null)
            {
                connection.UserServerId = userServerId;
            }
        }

        public static void ServerConnectionStateChanged(VisualCronSession session, string userServerId, bool connected)
        {
            var connection = session.Connections.Find(c => c.UserServerId == userServerId);

            if (connection != null)
            {
                connection.Connected = connected;
            }

            session.ConnectionStateChanged();
        }

        public static VCConnection GetConnection(VisualCronSession session, string connectionId)
        {
            return session.Connections.Find(c => c.ConnectionId.ToString() == connectionId);
        }

        public static List<VCConnection> GetNewConnectionsList()
        {
            return new List<VCConnection> { new VCConnection("localhost", null, null, null, true, false) };
        }

        #endregion
    }
}